package com.ft.adbix.utils;

import android.text.TextUtils;

import com.ft.adbix.core.common.AdType;
import com.ft.adbix.core.response.Ad;

public class AdChecker {

    //检查广告信息完整性
    public static boolean checkAdEnable(Ad ad) {
        if (ad==null){
            return false;
        }
        //基本信息检查
        if (TextUtils.isEmpty(ad.getAdType()) || ad.getActionType() < 0) {
            return false;
        }
        //资源检查
        boolean resEnable;
        if (ad.getAdType().equals(AdType.TYPE_FLOW)) {
            resEnable = checkFlowAdRes(ad);
        } else if (ad.getAdType().equals(AdType.TYPE_REWARD_VOD)) {
            resEnable = checkRewardAdRes(ad);
        } else {
            resEnable = checkFlowAdRes(ad);
        }
        //跳转条件检查
        boolean checkOpenEnable = checkOpenable(ad);
        return resEnable && checkOpenEnable;
    }

    //信息流广告素材
    private static boolean checkFlowAdRes(Ad ad) {
        return videoEnable(ad) || imgEnable(ad);
    }

    //激励广告素材
    private static boolean checkRewardAdRes(Ad ad) {
        return videoEnable(ad);
    }

    //判断视频资源是否可用
    public static boolean videoEnable(Ad ad) {
        if (ad.getVideo() != null) {
            return !TextUtils.isEmpty(ad.getVideo().getUrl());
        }
        return false;
    }

    //判断图片资源是否可用
    public static boolean imgEnable(Ad ad) {
        return (ad.getImageUrlList() != null && ad.getImageUrlList().length > 0) || !TextUtils.isEmpty(ad.getImageUrl());
    }

    //判断是否具备打开条件
    private static boolean checkOpenable(Ad ad) {
        //下载类需要appPromotion信息
        if (ad.getActionType() == 3) {
            if (ad.getAppPromotion() == null){
                return false;
            }
        }
        return !TextUtils.isEmpty(ad.getDeeplinkUrl()) || checkWxMiniOpenEnable(ad) || !TextUtils.isEmpty(ad.getLandingPageUrl());
    }

    //判断微信小程序信息
    private static boolean checkWxMiniOpenEnable(Ad ad) {
        return !(TextUtils.isEmpty(ad.getWechatId()) && TextUtils.isEmpty(ad.getWechatPath()));
    }
}
